package ksv.cron.v1.impl;

import api.common.Cst;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ksv.cron.CronConfig;
import ksv.cron.CronContext;
import ksv.cron.CronCst;
import ksv.cron.bean.SysCron;
import ksv.cron.v1.CronLoader;
import mybatis.mapper.CronMapper;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * @创建人 wsdc
 * @时间 2021/1/31
 * @描述 $
 */
public class MysqlCronLoader implements CronLoader {
    @Resource
    CronMapper cronMapper;

    LocalDateTime lastRequestTime = LocalDateTime.of(1970,3,1,0,0);

    @Override
    public Mono<List<SysCron>> provideCron(CronConfig cronConfig, LocalDateTime now) {
        return Mono.just(new LinkedList<>());
    }

    @Override
    public List<SysCron> provide(CronConfig cronConfig, LocalDateTime now) {
        List<SysCron> sysCrons = cronMapper.selectList(Wrappers.<SysCron>lambdaQuery()
                .ge(SysCron::getCreateTime, lastRequestTime)
                .in(SysCron::getHash, cronConfig.getHashList())
                .eq(SysCron::getStatus, CronCst.Status.COMMON)
        );

        //  时间偏移
        lastRequestTime = LocalDateTime.now().plusSeconds(-15);
        return sysCrons;
    }
}
